package com.cheng.utils;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.*;
import com.deepoove.poi.data.style.CellStyle;
import com.deepoove.poi.data.style.RowStyle;
import org.apache.commons.collections4.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Word工具类---模板+数据=输出
 * @author cheng-qiang
 * @date 2022年08月16日16:55
 */
public class WordUtils {

    public static String getPath() {
        return Objects.requireNonNull(WordUtils.class.getResource("/")).getPath();
    }

    /**
     * 导出带有动态表格的word文档---应用场景：文档中有且只有一个动态表格
     * @author cheng-qiang
     * @date 2022/8/16 17:33
     * @param templateFileName 模板文件名称
     * @param modelData 数据模型
     * @param fileName 文件名称
     * @param tagName 表格标记
     * @param startRow 起始位置
     * @param tableColumn 表格列数
     * @param response HttpServletResponse
     */
    public static void exportWordDocumentWithTables(String templateFileName,
                                             Object modelData,
                                             String fileName,
                                             String tagName,
                                             Integer startRow,
                                             Integer tableColumn,
                                             HttpServletResponse response){
            Configure configure = Configure.builder()
                    .bind(tagName, new DynamicTableRenderCustomPolicy(startRow,tableColumn))
                    .build();
            downloadWord(templateFileName, modelData, fileName, configure, response);
    }

    /**
     * 导出带有动态表格的word文档---应用场景：config 可以设置多个动态配置
     * @author cheng-qiang
     * @date 2022/8/16 17:43
     * @param templateFileName 模板文件名称
     * @param modelData 数据模型
     * @param fileName 文件名称
     * @param configure 动态配置
     * @param response HttpServletResponse
     */
    public static void exportWordDocumentWithTables(String templateFileName,
                                                    Object modelData,
                                                    String fileName,
                                                    Configure configure,
                                                    HttpServletResponse response){
            downloadWord(templateFileName, modelData, fileName, configure, response);
    }

    /**
     * 下载word文档
     * @author cheng-qiang
     * @date 2022/8/16 17:46
     * @param templateFileName 模板文件名称
     * @param modelData 数据模型
     * @param fileName 文件名称
     * @param configure 动态配置
     * @param response HttpServletResponse
     */
    private static void downloadWord(String templateFileName, Object modelData, String fileName, Configure configure, HttpServletResponse response)  {
        try {
            XWPFTemplate template = XWPFTemplate.compile(
                    URLDecoder.decode(getPath() + "templates" + File.separator  + templateFileName,"utf-8"),
                    configure).render(modelData);
            downloadWord(fileName, response, template);
        }catch (Exception ignored){

        }
    }

    /**
     * 导出word文档---应用场景：文档中不涉及动态配置Config
     * @author cheng-qiang
     * @date 2022/8/16 17:56
     * @param templateFileName 模板文件名称
     * @param modelData 数据模型
     * @param fileName 文件名称
     * @param response HttpServletResponse
     */
    public static void exportWordDocument(String templateFileName,
                                                    Object modelData,
                                                    String fileName,
                                                    HttpServletResponse response){
        try {
            XWPFTemplate template = XWPFTemplate.compile(
                    URLDecoder.decode(getPath() + "templates" + File.separator  + templateFileName,"utf-8")).render(modelData);
            downloadWord(fileName, response, template);
        }catch (Exception ignored){

        }
    }

    /**
     * 下载word文档
     * @author cheng-qiang
     * @date 2022/8/16 17:57
     * @param fileName 文件名称
     * @param response HttpServletResponse
     * @param template XWPFTemplate
     */
    private static void downloadWord(String fileName, HttpServletResponse response, XWPFTemplate template) throws IOException {
        response.setContentType("application/force-download");
        response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "utf-8"));
        OutputStream out = response.getOutputStream();
        template.write(out);
        out.flush();
        out.close();
        template.close();
    }

    /**
     * 渲染文本行数据附带下标---应用场景：设置文本序号编号
     * @author cheng-qiang
     * @date 2022/8/16 18:01
     * @param contents contents
     * @param numberingFormat NumberingFormat.DECIMAL--(1.)
     * @return com.deepoove.poi.data.NumberingRenderData
     */
    public static NumberingRenderData renderingNumberingRenderData(List<String> contents,NumberingFormat numberingFormat){
        if (CollectionUtils.isEmpty(contents)||contents.size()==0){
            return null;
        }
        TextRenderData[]  textRenderData = new TextRenderData[contents.size()];
        for (int i = 0; i < contents.size(); i++) {
            textRenderData[i] = new TextRenderData(contents.get(i));
        }
        NumberingRenderData numberingRenderData = Numberings.of(textRenderData).create();
        List<NumberingFormat> numberingFormatList = new ArrayList<>();
        numberingFormatList.add(numberingFormat);
        numberingRenderData.setFormats(numberingFormatList);
        return numberingRenderData;
    }

    /**
     * 渲染行数据背景颜色--应用场景：标记某一行数据
     * @author cheng-qiang
     * @date 2022/8/17 9:53
     * @param renderData 渲染数据
     * @param backgroundColor 背景颜色(十六进制)---ff0000
     * @return com.deepoove.poi.data.RowRenderData
     */
    public static RowRenderData rowRenderDataStyleBuilder(RowRenderData renderData, String backgroundColor){
        CellStyle cellStyle = new CellStyle();
        cellStyle.setBackgroundColor(backgroundColor);
        RowStyle rowStyle = new RowStyle();
        rowStyle.setDefaultCellStyle(cellStyle);
        renderData.setRowStyle(rowStyle);
        return renderData;
    }

}
